<?php
/*
PM popup window on jQuery-UI for phpBB3 by ab00
ab00web@gmail.com
http://ab00web.wordpress.com/
*/
if ($user->data['user_new_privmsg'] AND $user->optionget('popuppm'))
{
	$user->add_lang('mods/pm_alert');
	
	$sql = "SELECT 
			pm.message_subject, 
			pm.message_text, 
			u.username
			FROM 
			" . PRIVMSGS_TABLE . " AS pm,
			" . PRIVMSGS_TO_TABLE . " AS pmt, 
			" . USERS_TABLE ." AS u  
			WHERE
			pm.author_id = u.user_id
			AND pmt.msg_id = pm.msg_id
			AND pmt.author_id != '". $user->data['user_id'] ."'
			AND pmt.user_id = '". $user->data['user_id'] ."'
			ORDER BY message_time DESC
			LIMIT 1";	
			
	$result = $db->sql_query($sql, 1);
	$row = $db->sql_fetchrow($result);	
	
	$alert_text = ($user->data['user_unread_privmsg'] == 1) ? $user->lang['PM_ALERT'] : $user->lang['PM_ALERTS'];
	$alert_text = sprintf($alert_text, $user->data['username'], $user->data['user_unread_privmsg']);
	$inbox_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&folder=inbox");

	$pm_alert = "<div id=\"newpm\" title=\"".$user->lang['PM_ALERT_TITLE']."\" style=\"display:none;\" ><br />
				 ".$alert_text."
				 <br /><br />
				 " . $user->lang['TOPIC'] . ": <b>" . $row['message_subject'] . "</b><br />
				 " . $user->lang['PM_ALERT_FROM'] . ": <b>" . $row['username'] . "</b><br /><br />
				 <i>" . $row['message_text'] . "</i></div>";
				 
	$pm_alert .= '<script type="text/javascript">    
				  jQuery(function(){				  	
					jQuery("#newpm").dialog({
						autoOpen: true,
						show: \'fade\',
						hide: \'fade\',						
						width: 450,
						height: 260,
						buttons: {
							"' . $user->lang['CLOSE_PM_WINDOW'] . '": function() { 
							jQuery(this).dialog("close"); 
							}, 
							"' . $user->lang['GO_TO_INBOX'] . '": function() { 
								document.location="' . $inbox_url . '";	
							} 
						}
					});
				});
				</script>';
	$db->sql_freeresult($result);
}
?>